package com.xiaomo.gitee.datamanager.controller.user;

import com.xiaomo.gitee.datamanager.common.result.Code;
import com.xiaomo.gitee.datamanager.common.result.Result;
import com.xiaomo.gitee.datamanager.dto.tag.TagDTO;
import com.xiaomo.gitee.datamanager.entity.ImageTag;
import com.xiaomo.gitee.datamanager.entity.Tags;
import com.xiaomo.gitee.datamanager.service.tag.CategoryTagService;
import com.xiaomo.gitee.datamanager.service.tag.ImageTagService;
import com.xiaomo.gitee.datamanager.service.tag.TagsService;
import com.xiaomo.gitee.datamanager.service.tag.impl.CategoryTagServiceImpl;
import com.xiaomo.gitee.datamanager.service.tag.impl.ImageTagServiceImpl;
import com.xiaomo.gitee.datamanager.service.tag.impl.TagsServiceImpl;
import com.xiaomo.gitee.datamanager.util.JwtUtils;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
@Tag(name = "获取标签信息",description = "用于查询图片标签、数据集标签")
@RequiredArgsConstructor
public class UserTagController {

    private final ImageTagService imageTagService;
    private final TagsService tagsService;
    private final CategoryTagService categoryTagService;

    @GetMapping("/image-tag/{id}")
    @PreAuthorize("hasAuthority('tag_info')")
    @Operation(
            summary = "获取图片标签",
            description = "通过图片ID获取图片标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "图片ID",
                            required = true,
                            in = ParameterIn.PATH
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "获取成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = TagDTO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<List<TagDTO>> getImageTags(@PathVariable String id) {
        List<ImageTag> imageTagList = imageTagService.getTagsOfImage(id);
        List<TagDTO> resultList = new ArrayList<>();
        for (ImageTag imageTag : imageTagList) {
            TagDTO tagDto = new TagDTO();
            tagDto.setId(imageTag.getTagId());
            tagDto.setContent(tagsService.getTagById(imageTag.getTagId()).getContent());
            tagDto.setIsCategory(imageTag.isBelongClass());
            resultList.add(tagDto);
        }
        return Result.ok(resultList);
    }

    @GetMapping("/category-tag/{id}")
    @PreAuthorize("hasAuthority('tag_info')")
    @Operation(
            summary = "获取数据集标签",
            description = "通过数据集id获取数据集的所欲标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            parameters = {
                    @Parameter(
                            name = "id",
                            description = "数据集ID",
                            required = true,
                            in = ParameterIn.PATH
                    )
            },
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "获取成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = TagDTO.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<List<TagDTO>> getClassTags(@PathVariable Long id) {
        return Result.ok(categoryTagService.getTagsOfSet(id).stream().map(tag -> new TagDTO(tag.getId(),tag.getContent(),true)).toList());
    }


    @GetMapping("/tag")
    @PreAuthorize("hasAuthority('tag_info')")
    @Operation(
            summary = "获取所有标签",
            security = @SecurityRequirement(name = JwtUtils.TOKEN_HEADER),
            responses = {
                    @ApiResponse(
                            responseCode = Code.SUCCESS,
                            description = "获取成功",
                            content = @Content(
                                    mediaType = "application/json",
                                    array = @ArraySchema(
                                            schema = @Schema(
                                                    implementation = Tags.class
                                            )
                                    )
                            )
                    )
            }
    )
    public Result<List<Tags>> getAllTags() {
        return Result.ok(tagsService.getAllTags());
    }

    @PostMapping("/getMultipleImageTags")
    @PreAuthorize("hasAuthority('tag_info')")
    @ResponseBody
    @Hidden
    @Deprecated
    public List<List<String>> getMultipleImageTags(@RequestBody List<String> data) {
        return imageTagService.getMultipleTags(data);
    }


}
